package com.test.rabbitmq;

import com.rabbitmq.client.Channel;
import com.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * @author : caili
 * @date : 10:28 2023/1/14
 */

@Component
@Slf4j
public class NormalReceiver {

    //normalQueue 普通模式下设置处理方式为手动ack 设置并发数为4
    @RabbitListener(
            queuesToDeclare = @Queue("normalQueue"),ackMode = "MANUAL",concurrency = "4"
    )
    public void process(String pMessage, Message message, Channel channel) throws IOException {
        log.info("[普通模式]接收生产中消息:{}",pMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try{
            // 演示逻辑处理 暂停1S
            TestService testService = new TestService();
            TimeUnit.SECONDS.sleep(1);
            boolean flag = testService.toHandle();
            log.info("业务处理返回值=>"+flag);
            log.info("["+System.currentTimeMillis()+"]业务处理成功,手动应答");
            //设置手动应答
            channel.basicAck(deliveryTag,false);
            /*if(!flag){
                log.info("业务处理失败,重入队列--------");
                channel.basicNack(deliveryTag,false,true);
            }else{

            }*/
        }catch (Exception e){
            channel.basicNack(deliveryTag,false,true);
        }

    }
}
